<?php
namespace app\admin\service;

use think\Loader;
use app\admin\service\Common;
use app\admin\model\Stock AS StockModel;
use app\admin\model\FinanceCashFlow AS FinanceCashFlowModel;

class FinanceCashFlow extends Common {

    const PROFIT_FORECASST_START = "https://datacenter.eastmoney.com/securities/api/data/get?type=RPT_F10_FINANCE_GCASHFLOW&sty=APP_F10_GCASHFLOW&filter=";
    const PROFIT_FORECASST_END = "&p=1&ps=5&sr=-1&st=REPORT_DATE&source=HSF10&client=PC&v=";
    public function _initialize(){
    }

    public static function Collect($stockId = 0, $arr = []){

        $year = intval(date("Y"));
        $month = intval(date("m"));
        $day = intval(date("d"));
        $dataTime = '';
        if($month <= 12 && $month >= 9){
            $dataTime = $year."-06";
        }else if($month <= 9 && $month >= 6){
            $dataTime = $year."-03";
        }else if($month <= 3){
            $dataTime = ($year-1)."-12";
        }
        $dataTime .= "-30";
        $url = self::PROFIT_FORECASST_START.'('.urlencode('SECUCODE="'.$arr['stock_code'].'.'.$arr['jys'].'"').')';
        $url .= "(REPORT_DATE%20in%20(".urlencode("'".$dataTime."'").'))'.self::PROFIT_FORECASST_END;
        $reFinanceCashFlowJson = "";
        $reFinanceCashFlowJson = file_get_contents($url);
        if(!empty($reFinanceCashFlowJson)){
            $reFinanceCashFlowJson = json_decode($reFinanceCashFlowJson,true);
        }

        if(!empty($reFinanceCashFlowJson['result']['data'][0])){
            $reFinanceCashFlowJson = $reFinanceCashFlowJson['result']['data'][0];

            $data = [];
            //经营活动现金流入小计
            $data['subtotal_cash_inflows_operat_activitie'] = 0;
            if(!empty($reFinanceCashFlowJson['TOTAL_OPERATE_INFLOW'])){
                $data['subtotal_cash_inflows_operat_activitie'] = $reFinanceCashFlowJson['TOTAL_OPERATE_INFLOW'];
            }
            //经营活动现金流出小计
            $data['subtotal_cash_outflow_operatactivitie'] = 0;
            if(!empty($reFinanceCashFlowJson['TOTAL_OPERATE_OUTFLOW'])){
                $data['subtotal_cash_outflow_operatactivitie'] = $reFinanceCashFlowJson['TOTAL_OPERATE_OUTFLOW'];
            }
            //经营活动产生的现金流量净额
            $data['net_cash_flow_operat_activitie'] = 0;
            if(!empty($reFinanceCashFlowJson['NETCASH_OPERATE'])){
                $data['net_cash_flow_operat_activitie'] = floatval($reFinanceCashFlowJson['NETCASH_OPERATE']);
            }
            //投资活动现金流入小计
            $data['subtotal_cash_inflows_investment_activitie'] = 0;
            if(!empty($reFinanceCashFlowJson['TOTAL_INVEST_INFLOW'])){
                $data['subtotal_cash_inflows_investment_activitie'] = floatval($reFinanceCashFlowJson['TOTAL_INVEST_INFLOW']);
            }
            //投资活动现金流出小计
            $data['subtotal_cash_outflow_investment_activitie'] = 0;
            if(!empty($reFinanceCashFlowJson['TOTAL_INVEST_OUTFLOW'])){
                $data['subtotal_cash_outflow_investment_activitie'] = floatval($reFinanceCashFlowJson['TOTAL_INVEST_OUTFLOW']);
            }
            //投资活动产生的现金流量净额
            $data['net_cash_flow_investment_activitie'] = 0;
            if(!empty($reFinanceCashFlowJson['NETCASH_INVEST'])){
                $data['net_cash_flow_investment_activitie'] = floatval($reFinanceCashFlowJson['NETCASH_INVEST']);
            }
            //筹资活动现金流入小计
            $data['subtotal_cash_inflow_financ_activitie'] = 0;
            if(!empty($reFinanceCashFlowJson['TOTAL_FINANCE_INFLOW'])){
                $data['subtotal_cash_inflow_financ_activitie'] = floatval($reFinanceCashFlowJson['TOTAL_FINANCE_INFLOW']);
            }
            //筹资活动现金流出小计
            $data['subtotal_cash_outflow_financ_activitie'] = 0;
            if(!empty($reFinanceCashFlowJson['TOTAL_FINANCE_OUTFLOW'])){
                $data['subtotal_cash_outflow_financ_activitie'] = floatval($reFinanceCashFlowJson['TOTAL_FINANCE_OUTFLOW']);
            }
            //筹资活动产生的现金流量净额
            $data['net_cash_flows_financ_activitie'] = 0;
            if(!empty($reFinanceCashFlowJson['NETCASH_FINANCE'])){
                $data['net_cash_flows_financ_activitie'] = floatval($reFinanceCashFlowJson['NETCASH_FINANCE']);
            }
            //汇率变动对现金及现金等价物的影响
            $data['effect_exchange_rate_changes_cash_cash_equivalent'] = 0;
            if(!empty($reFinanceCashFlowJson['RATE_CHANGE_EFFECT'])){
                $data['effect_exchange_rate_changes_cash_cash_equivalent'] = floatval($reFinanceCashFlowJson['RATE_CHANGE_EFFECT']);
            }
            //现金及现金等价物净增加额
            $data['net_increase_cash_cash_equivalent'] = 0;
            if(!empty($reFinanceCashFlowJson['CCE_ADD'])){
                $data['net_increase_cash_cash_equivalent'] = floatval($reFinanceCashFlowJson['CCE_ADD']);
            }
            //期末现金及现金等价物余额
            $data['clos_cash_equivalent_balance'] = 0;
            if(!empty($reFinanceCashFlowJson['END_CASH'])){
                $data['clos_cash_equivalent_balance'] = floatval($reFinanceCashFlowJson['END_CASH']);
            }
            //经营活动产生的现金流量净额
            $data['net_cash_flow_operat_activitie'] = 0;
            if(!empty($reFinanceCashFlowJson['NETCASH_OPERATENOTE'])){
                $data['net_cash_flow_operat_activitie'] = floatval($reFinanceCashFlowJson['NETCASH_OPERATENOTE']);
            }
            //现金及现金等价物的净增加额
            $data['net_increase_cash_equivalent'] = 0;
            if(!empty($reFinanceCashFlowJson['CCE_ADDNOTE'])){
                $data['net_increase_cash_equivalent'] = floatval($reFinanceCashFlowJson['CCE_ADDNOTE']);
            }
            $data['remark'] = json_encode($reFinanceCashFlowJson);
            $data['sid'] = $stockId;
            $where = ['sid'=>$stockId];
            if(FinanceCashFlowModel::Detail($where)){
                FinanceCashFlowModel::where($where)->update($data);
            }else{
                FinanceCashFlowModel::insert($data);
            }
        }
        return 1;
    }
}